//====================================================================================================
// Includes
//====================================================================================================

#include "BossWalk.h"

#include "Boss.h"

//====================================================================================================
// Class Definitions
//====================================================================================================

BossWalk::BossWalk(Boss* pOwner)
	: BossState(pOwner, "Walk")
{
	// Empty
}

//----------------------------------------------------------------------------------------------------

BossWalk::~BossWalk()
{
	// Empty
}

//----------------------------------------------------------------------------------------------------

void BossWalk::Load(int level)
{
	mSprite.Add("boss_walk01.png");
	mSprite.Add("boss_walk01.png");
	mSprite.Add("boss_walk02.png");
	mSprite.Add("boss_walk02.png");
	mSprite.Add("boss_walk03.png");
	mSprite.Add("boss_walk03.png");
	mSprite.Add("boss_walk04.png");
	mSprite.Add("boss_walk04.png");
	mSprite.Add("boss_walk05.png");
	mSprite.Add("boss_walk05.png");
}

//----------------------------------------------------------------------------------------------------

void BossWalk::Unload()
{
	mSprite.Unload();
}

//----------------------------------------------------------------------------------------------------

void BossWalk::Update(float deltaTime)
{
	// Update sprite
	mSprite.Update(deltaTime);

	// State transitions
	const float kSpeed = 300.0f;

	// ---------------------------------
	mDelay -= deltaTime;
	float random = (rand()%2);

	// move left
	if ( random == 0 )
	{
		if ( mDelay <= 0 )
		{
			SVector2 vel(-kSpeed * deltaTime, 0.0f);
			mpOwner->SetVelocity(vel);
			mpOwner->SetFacingLeft(true);
			mDelay = RandomFloat(1.0f, 2.0f);
		}
	}

	// move right
	else if ( random == 1 )
	{
		if ( mDelay <= 0 )
		{
			SVector2 vel(kSpeed * deltaTime, 0.0f);
			mpOwner->SetVelocity(vel);
			mpOwner->SetFacingLeft(false);
			mDelay = RandomFloat(1.0f, 2.0f);
		}
	}

}

//----------------------------------------------------------------------------------------------------

void BossWalk::Render(const SVector2& offset)
{
	const int kTextureWidth = mSprite.GetWidth();
	const int kTextureHeight = mSprite.GetHeight();
	const SVector2 ownerPos(mpOwner->GetPosition());
	const SVector2 renderPos(ownerPos.x - (kTextureWidth * 0.5f), ownerPos.y - kTextureHeight);
	bool facingLeft = mpOwner->IsFacingLeft();
	mSprite.SetPosition(renderPos - offset);
	mSprite.SetFlipH(facingLeft);
	mSprite.Render();
}

//----------------------------------------------------------------------------------------------------

void BossWalk::Enter()
{
	mSprite.Play(4.0f, true);
}

//----------------------------------------------------------------------------------------------------

void BossWalk::Exit()
{
	mSprite.Stop();
	mSprite.Reset();
}

//----------------------------------------------------------------------------------------------------

SRect BossWalk::GetBoundingBox() const
{
	const SVector2 pos(mpOwner->GetPosition());
	return SRect
	(
		pos.x - 16.0f,
		pos.y - 56.0f,
		pos.x + 16.0f,
		pos.y
	);
}